import React,{Component} from 'react';
import { Link, withRouter } from 'react-router-dom';
import{ connect} from 'react-redux';
import { Menu} from 'antd';
import {
    HomeOutlined,
    MailOutlined,
  } from '@ant-design/icons';
  import { createSaveAction } from '../../../redux/action_creators/menu_action'
  import logo from '../../../static/images/logo.png';
  import adminMenu  from '../../../config/adminMenu';
  import teacherMenu from '../../../config/teacherMenu';
  import './leftNav.less';
const { SubMenu, Item } = Menu;
@connect(
    state=>({
        user:state.userInfo.user,
    }),
    {
        saveTitle:createSaveAction,
    }
)
@withRouter
class LeftNav extends Component {
    // 用于创建菜单的函数
    createMenu = (target) =>{
        return  target.map((item)=>{
            if(!item.children){
                return(
                    <Item  key={item.key} onClick={()=>{ this.props.saveTitle(item.title)}} icon={ <HomeOutlined/>}>
                        <Link to={item.path}>{item.title}</Link>
                    </Item> 
                )
            }else{
                return(
                    <SubMenu key={item.key} icon={<MailOutlined />} title={item.title}>
                        { this.createMenu(item.children)}
                    </SubMenu>
                )  
            }
        })
    }
    render(){
        const {pathname} =this.props.location
        const { authority } = this.props.user
        return (
            <div >
                <header className="nav-header">
                    <img src={logo} alt="logo"/>
                </header>  
                <Menu
                selectedKeys={
                    pathname.indexOf('content') !== -1 ?
                     'content':
                     (pathname.indexOf('push/notice') !== -1 ? 'notice':
                     (pathname.indexOf('push/article') !== -1 ? 'article':
                     (pathname.indexOf('admin/teacher') !== -1 ? 'teacher':
                     (pathname.indexOf('curriculum') !== -1 ? 'curriculum':
                     (pathname.indexOf('subject') !== -1 ? 'subject':
                     (pathname.indexOf('info') !== -1 ? 'info':
                      pathname.split('/').reverse()[0]))))))
                    }
                defaultOpenKeys={pathname.split('/').splice(2)}
                mode="inline"
                theme="dark"
                >
                    {authority===undefined?this.createMenu(teacherMenu):this.createMenu(adminMenu)}
                </Menu>
            </div>
           
        );
    }
}
export default LeftNav;